<?php
/**
 * 颜色选择
 * @author yupoxiong<i@yufuping.com>
 */

namespace generate\field;

class Color extends Field
{
    public static $html = <<<EOF
<div class="layui-form-item">
    <label for="[FIELD_NAME]" class="layui-form-label  [REQUIRED]">[FORM_NAME]</label>
    <div class="layui-input-inline">
        <div  id="color-[FIELD_NAME]"> </div>

        <div class="layui-input-inline" style="width: 120px;">
        <input type="text" value="{\$data.[FIELD_NAME]|default='#009688'}"  lay-verify="[FORM_VALIDATE]"  name="[FIELD_NAME]" placeholder="请选择[FORM_NAME]" class="layui-input"
            id="color-[FIELD_NAME]-input">
    </div>
    <div class="layui-inline" style="left: -11px;">
        <div id="color-[FIELD_NAME]"></div>
    </div>
    </div>
</div>
<script>
layui.use('colorpicker', function () {
    var colorpicker = layui.colorpicker;
      colorpicker.render({
        elem: '#color-[FIELD_NAME]'
        , color: "{\$data.[FIELD_NAME]|default='#009688'}"
        , done: function (color) {
            $('#color-[FIELD_NAME]-input').val(color);
        }
    });
});
</script>\n
EOF;

    public static $rules = [
        'required' => '非空',
        'color'    => '颜色',
        'regular'  => '自定义正则'
    ];


    public static function create($data)
    {
        self::getValidateHtml($data['form_validate']);

        return str_replace(array('[FORM_NAME]', '[FIELD_NAME]', '[FORM_VALIDATE]', '[REQUIRED]'), array($data['form_name'], $data['field_name'], self::$verify, self::$required), self::$html);
    }
}